package org.footballmanager.pandafootball.system.controller;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootQd;
import org.footballmanager.pandafootball.system.domain.JsonReturn;
import org.footballmanager.pandafootball.system.form.impl.FootQdSearchForm;
import org.footballmanager.pandafootball.system.service.FootQdService;
import org.footballmanager.pandafootball.system.service.FootSslsService;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@SessionAttributes("footQdSearchForm")
@RequestMapping("/foot/footQd")
public class FootQdController extends AddAndEditController<FootQd> {
    /* 实体名称 */
    private static final String ENTITYNAME = "footQd";
    private static final String ADDPAGE = String.format(Constants.ADDPAGE, Constants.FOOT, ENTITYNAME);
    private static final String EDITPAGE = String.format(Constants.EDITPAGE, Constants.FOOT, ENTITYNAME);
    private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.FOOT, ENTITYNAME);
    private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.FOOT, ENTITYNAME);
    private static final String REDIRECT = String.format(Constants.REDIRECT, Constants.FOOT, ENTITYNAME);

    @Autowired
    private FootQdService footQdService;
    @Autowired
    private FootSslsService footSslsService;
    @Autowired
    private FootUserService footUserService;

    /**
     * 抽象方法
     *
     * @return 实体名称
     */
    @Override
    protected String getEntityName() {
        return ENTITYNAME;
    }

    /**
     * 初始化
     *
     * @param map              参数
     * @param footQdSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootQdSearchForm footQdSearchForm) {
        map.addAttribute("sslsList", footSslsService.getall());
        map.addAttribute("userList", footUserService.getall());
        FootQdSearchForm newFootQdSearchForm = new FootQdSearchForm();
        if (footQdSearchForm != null) {
            newFootQdSearchForm = footQdSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootQdSearchForm);
    }

    @RequestMapping(value = "/search")
    public String search(Model map, @ModelAttribute FootQdSearchForm footQdSearchForm) {
        footQdService.search(ENTITYNAME, map, footQdSearchForm);
        return LISTPAGE;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(@RequestParam Long id, HttpServletRequest request) {
        footQdService.delete(id, request);
        return REDIRECT;
    }

    @RequestMapping(value = "/deleteall", method = RequestMethod.POST)
    public String deleteall(HttpServletRequest request, @ModelAttribute FootQdSearchForm footQdSearchForm) {
        footQdService.deleteAll(footQdSearchForm, request);
        return REDIRECT;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String openAddPage(Model map) {
        map.addAttribute(ENTITYNAME, new FootQd());
        return ADDPAGE;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String openEditPage(Model map, @RequestParam Long id) {
        map.addAttribute(ENTITYNAME, footQdService.get(id));
        return EDITPAGE;
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String openViewPage(Model map, @RequestParam Long id, @ModelAttribute FootQdSearchForm footQdSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footQdSearchForm);
        map.addAttribute(ENTITYNAME, footQdService.get(id));
        return VIEWPAGE;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public JsonReturn update(@RequestParam Long id, @RequestParam String qdjj, HttpServletRequest request) {
        if (id == null) {
            return new JsonReturn(Constants.ERROR);
        }
        FootQd footQd = footQdService.get(id);
        if (footQd == null) {
            return new JsonReturn(Constants.ERROR);
        }
        footQd.setQdjj(qdjj);
        footQdService.updateNotNull(footQd, request);
        return new JsonReturn(Constants.OK);
    }
}
